package com.tuanzi.loan.web.bean.system;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.TreeDragDropEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.system.FunctionalPermission;
import com.tuanzi.loan.business.entity.system.RelationType;
import com.tuanzi.loan.business.service.system.FunctionalPermissionRelationService;
import com.tuanzi.loan.business.service.system.FunctionalPermissionService;
import com.tuanzi.loan.business.service.system.RoleService;
import com.tuanzi.loan.business.service.system.SystemUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.system.RoleSearchRequest;
import com.tuanzi.loan.vo.request.system.SystemUserSearchRequest;
import com.tuanzi.loan.vo.response.system.RoleSearchResponse;
import com.tuanzi.loan.vo.response.system.SystemUserSearchResponse;
import com.tuanzi.loan.web.configuration.JSFInfoBean;
import com.tuanzi.loan.web.domain.PermissionDomain;
import com.tuanzi.loan.web.lazy.system.RoleLazyDataModel;
import com.tuanzi.loan.web.lazy.system.SystemUserLazyDataModel;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class FunctionalPermissionBean extends JSFInfoBean<FunctionalPermission> {

	private static final long serialVersionUID = 6059675680981337738L;
	@Autowired
	private FunctionalPermissionService functionalPermissionService;
	@Autowired
	private FunctionalPermissionRelationService functionalPermissionRelationService;
	@Autowired
	private SystemUserService userService;
	@Autowired
	private RoleService roleService;

	private PermissionDomain permissionDomain;

	private TreeNode root;
	private TreeNode selectedNode;
	private FunctionalPermission selectedPermission;

	private RelationType relationType;
	private SystemUserSearchRequest userSearchRequest;
	private RoleSearchRequest roleSearchRequest;
	private LazyDataModel<SystemUserSearchResponse> userLazyDataModel;
	private LazyDataModel<RoleSearchResponse> roleLazyDataModel;

	@PostConstruct
	private void init() {
		permissionDomain = new PermissionDomain();
		root = permissionDomain.generateFunctionalPermissionTreeNode();
		userSearchRequest = new SystemUserSearchRequest();
		roleSearchRequest = new RoleSearchRequest();
		userLazyDataModel = new SystemUserLazyDataModel(userSearchRequest);
		roleLazyDataModel = new RoleLazyDataModel(roleSearchRequest);
	}

	public void onChangeUserMode() {
		relationType = RelationType.USER;
		userSearchRequest.setFunctionalPermissionId(selectedPermission.getId());
	}

	public void onChangeRoleMode() {
		relationType = RelationType.ROLE;
		roleSearchRequest.setFunctionalPermissionId(selectedPermission.getId());
	}

	public void onGenerateFrequentMenu() {
		functionalPermissionService.generateFrequentPermission(selectedPermission);
		addSuccessfullyMessage();
		refresh();
	}

	public void onSelectNode() {
		relationType = null;
		FunctionalPermission functionalPermission = (FunctionalPermission) selectedNode.getData();
		if (functionalPermission.getId() != null) {
			selectedPermission = functionalPermission;
			$ = selectedPermission;
		} else {
			selectedPermission = null;
			$ = null;
		}
		changeStatusToView();
	}

	public void onAddPermission() {
		relationType = null;
		if (selectedNode == null) {
			addWarnMessage("请选择权限节点");
			return;
		}
		$ = new FunctionalPermission();
		changeStatusToAdd();
	}

	public void onDisable() {
		functionalPermissionService.disable($);
		refresh();
		addSuccessfullyMessage();
	}

	public void onEnable() {
		functionalPermissionService.enable($);
		refresh();
		addSuccessfullyMessage();
	}

	public void onDelete() {
		functionalPermissionService.recurseDelete($);
		addSuccessfullyMessage();
		refresh();
	}

	public void onMovePermission(TreeDragDropEvent event) {
		FunctionalPermission dragPermission = (FunctionalPermission) event.getDragNode().getData();
		FunctionalPermission dropPermission = (FunctionalPermission) event.getDropNode().getData();
		functionalPermissionService.movePermission(dragPermission, dropPermission);
		addSuccessfullyMessage();
		refresh();
	}

	public void onSavePermission() {
		if ($.getId() == null && selectedPermission != null) {
			$.setParentId(selectedPermission.getId());
		}
		functionalPermissionService.saveOrUpdate($);
		addSuccessfullyMessage();
		changeStatusToView();
		refresh();
	}

	public void onCancel() {
		$ = selectedPermission;
		changeStatusToView();
	}

	public void onRelateUser(String userId) {
		functionalPermissionRelationService.relate(selectedPermission.getId(), RelationType.USER, userId);
		addSuccessfullyMessage();
	}

	public void onUnrelateUser(String userId) {
		functionalPermissionRelationService.unrelate(selectedPermission.getId(), RelationType.USER, userId);
		addSuccessfullyMessage();
	}

	public void onRelateRole(String roleId) {
		functionalPermissionRelationService.relate(selectedPermission.getId(), RelationType.ROLE, roleId);
		addSuccessfullyMessage();
	}

	public void onUnrelateRole(String roleId) {
		functionalPermissionRelationService.unrelate(selectedPermission.getId(), RelationType.ROLE, roleId);
		addSuccessfullyMessage();
	}

	private void refresh() {
		root = permissionDomain.generateFunctionalPermissionTreeNode();
		selectedNode = null;
		$ = null;
		relationType = null;
	}
}